import https from "https";
import fs from "fs";
import cheerio, { Cheerio } from "cheerio";

class main {
  // 三体第一部
  //   public url = "https://www.shizongzui.cc/santi/282.html";
  //   第二部
  //   public url = "https://www.shizongzui.cc/santi/318.html";
  //   第三部
  public url = "https://www.shizongzui.cc/santi/368.html";
  
  constructor() {
    this.onStart(this.url);
  }
  onStart(url: string) {
    const self = this;
    let list: Uint8Array[] = [],
      buffer = null,
      html = "";
    const req = https.request(url, (res) => {
      res.on("data", (data: Uint8Array) => {
        list.push(data);
      });
      res.on("end", () => {
        buffer = Buffer.concat(list);
        html = buffer.toString();
        const $ = cheerio.load(html);
        const title = ($("h1")[1].children[0] as any)["data"];
        const nextUrl = ($("h2")[0].children.slice(-1)[0] as any).attribs.href;
        const content = $("#BookText")[0] as any;
        console.log(title);

        const data = content.children.map((item: any) => {
          const { data } = item;

          if (!!data) {
            return data;
          } else {
            return "";
          }
        }) as string[];

        data.unshift(`    ${title}\n`);

        fs.appendFile(
          `./xiaoshuo/txt/${"三体"}.txt`,
          data.join("\n"),
          (err) => {
            console.log("写入完毕", err);
          }
        );
        self.onStart(nextUrl);
      });
      res.on("error", (err) => {
        console.log(err);
      });
    });
    req.end();
  }
}

new main();
